package com.px.handler;

import com.alibaba.fastjson2.JSON;
import com.px.common.core.redis.RedisCache;
import com.px.common.utils.DateUtils;
import com.px.device.domain.AirportInfo;
import com.px.device.domain.dto.AirportDto;
import com.px.device.domain.dto.SubDevices;
import com.px.device.mapper.AirportInfoMapper;
import com.px.framework.mqtt.handler.IMqttHandler;
import com.px.system.domain.CommonTopic;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

import static com.px.framework.mqtt.model.TopicConst.*;
import static com.px.framework.mqtt.model.TopicConst.STATUS_SUF;

@Slf4j
@Component
public class SysStatusMqttHandler extends IMqttHandler {

    @Autowired
    private AirportInfoMapper airportInfoMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public Pattern bindTopicName() {
        return Pattern.compile("^sys/product/" + REGEX_SN + "/status$");
    }

    //设备上下线、更新拓扑
    @Override
    public void handler(String topic, MqttMessage mqttMessage){
        String sn = topic.substring((BASIC_PRE + PRODUCT).length(),
                topic.indexOf(STATUS_SUF));
        CommonTopic ct = JSON.parseObject(mqttMessage.getPayload(), CommonTopic.class);
        switch (ct.getMethod()){
            case "update_topo" :
                AirportDto airportDto = JSON.parseObject(ct.getData().toJSONString(),AirportDto.class);
                //先把子设备循环插入 -- 一个机场只有一台子设备
                airportDto.getSub_devices().forEach(subDevices ->{
                    inorUpAirportOfSubDevices(subDevices);
                });
                AirportInfo airport = airportInfoMapper.selectAirportInfoByAirportSn(sn);
                if(airport != null){

                    if(airportDto.getSub_devices().size() > 0){
                        SubDevices subDevices= airportDto.getSub_devices().get(0);
                        String model = subDevices.getDomain() + "-" + subDevices.getType() + "-" + subDevices.getSub_type();
                        airport.setAirportChildSn(subDevices.getSn());
                        airport.setAirportChildMode(model);
                    }
                    //查询机场是否存在，存在更新，不存在插入
                    airport.setUpdateTime(DateUtils.getNowDate());
                    airportInfoMapper.updateAirportInfo(airport);
                    redisCache.setCacheObject(sn,0);
                    break;
                }
                String model = airportDto.getDomain() + "-" + airportDto.getType() + "-" + airportDto.getSub_type();
                AirportInfo airportInfo = new AirportInfo();
                airportInfo.setAirportSn(sn);
                airportInfo.setAirportModel(model);
                airportInfo.setAirportName(sn);
                airportInfo.setCreateTime(DateUtils.getNowDate());
                if(airportDto.getSub_devices().size() > 0){
                    SubDevices subDevices= airportDto.getSub_devices().get(0);
                    String subModel = subDevices.getDomain() + "-" + subDevices.getType() + "-" + subDevices.getSub_type();
                    airport.setAirportChildSn(subDevices.getSn());
                    airport.setAirportChildName(subDevices.getSn());
                    airport.setAirportChildMode(subModel);
                }
                airportInfoMapper.insertAirportInfo(airportInfo);
                redisCache.setCacheObject(sn,0);
                break;
            default:

                break;
        }
    }

    public void inorUpAirportOfSubDevices(SubDevices subDevices) {
        //查询设备是否存在，存在更新，不存在插入
        AirportInfo airport = airportInfoMapper.selectAirportInfoByAirportSn(subDevices.getSn());
        String model = subDevices.getDomain() + "-" + subDevices.getType() + "-" + subDevices.getSub_type();
        //entity插入值
        if (airport != null){
            airport.setUpdateTime(DateUtils.getNowDate());
            airportInfoMapper.updateAirportInfo(airport);
            return;
        }
        AirportInfo entity = new AirportInfo();
        entity.setAirportSn(subDevices.getSn());
        entity.setAirportModel(model);
        entity.setAirportName(subDevices.getSn());
        entity.setCreateTime(DateUtils.getNowDate());
        airportInfoMapper.insertAirportInfo(entity);
    }

}
